/**
 * @ignore
 */
Ext.data.ProxyMgr.registerType("workspacestorage",
    Ext.extend(Ext.data.LocalStorageProxy, {
    	id  : 'netgis-workspace-storage',
        update: function(operation, callback, scope) {
        	alert(operation);
        },
        proxy: {
        	idProperty: 'id'
        },
        autoSave: true
    	
		/*
        ,create: function(operation, callback, scope) {
        	Ext.data.LocalStorageProxy.create.call(this, operation, callback, scope);
        }

        ,read: function(operation, callback, scope) {
            var thisProxy = this;
        }

        ,destroy: function(operation, callback, scope) {
        }
        */
    })
);

/**
 * @class NG.models.Workspace
 * @extends Ext.data.Model
 * 
 * Model represents the categories of map.<br>
 * Model of workspace is registered as 'NG.models.Workspace'.<br>
 * Store of workspace is registered as 'workspacestorage'.
 * 
 */
NG.models.Workspace = Ext.regModel("NG.models.Workspace", {
	
	/**
	 * Fields:<br>
	 * <li>id
	 * <li>name
	 * <li>host
	 * <li>username
	 * <li>password
	 * <li>projectWholeWorld
	 * <li>tileSizeInPixel
	 * <li>getMapProxyUrl
	 * 
	 * @property fields
	 * @type Array
	 */
    fields: [
        {name: "id", type: "int"},
        {name: "name", type: "string"},
        {name: "host", type: "string"},
        {name: "username", type: "string"},
        {name: "password", type: "string"},
        {name: "projectWholeWorld", type: "string"},
        {name: "tileSizeInPixel", type: "int"},
        {name: "getMapProxyUrl", type: "string", defaultValue:null}
   	],
	validations: [
        {type: 'presence', name: 'name',message:"Proje Adı Giriniz"}
        ,{type: 'presence', name: 'host',message:"Url Giriniz"}
        ,{type: 'presence', name: 'username', message : "Kullanıcı Adı Giriniz"}
        //,{type: 'format',   name: 'email', matcher: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/, message:"Wrong Email Format"}
	],
   	//phantom: true,
	
	/**
	 * @field {Object} proxy
	 */
   	proxy: {
        type: 'localstorage',
        id  : 'netgis-workspace-storage'
    },
   	
   	/**
	 * 
	 * @return {String}
	 */
   	getId: function (){return this.data.id;},

   	/**
	 * 
	 * @return {String}
	 */
   	getName: function (){return this.data.name;},

   	/**
	 * 
	 * @return {String}
	 */
   	getHost: function (){return this.data.host;},
   	
   	/**
   	 * 
   	 * @param {String} host
   	 */
   	setHost: function (host){
   		this.data.host = host;
   	},


   	/**
	 * 
	 * @return {String}
	 */
   	getUsername: function (){return this.data.username;},
   	
   	/**
	 * 
	 * @return {String}
	 */
   	getPassword: function (){return this.data.password;},

   	/**
	 * 
	 * @return {String}
	 */
   	getProjectWholeWorld: function (){return this.data.projectWholeWorld;},

	/**
	 * It is retrieved from projectWholeWorld property. First number.
	 * @return {Number}
	 */
   	getBoundsLeft: function (){
		return this.data.projectWholeWorld.split(",")[0];
	},

	/**
	 * It is retrieved from projectWholeWorld property. Second number.
	 * @return {Number}
	 */
	getBoundsBottom: function (){
		return this.data.projectWholeWorld.split(",")[1];
	},

	/**
	 * It is retrieved from projectWholeWorld property. Third number.
	 * @return {Number}
	 */
	getBoundsRight: function (){
		return this.data.projectWholeWorld.split(",")[2];
	},

	/**
	 * It is retrieved from projectWholeWorld property. Fourth number.
	 * @return {Number}
	 */
	getBoundsTop: function (){
		return this.data.projectWholeWorld.split(",")[3];		
	},
   	
	/**
	 * 
	 * @return {Number}
	 */
   	getTileSizeInPixel: function (){return this.data.tileSizeInPixel;},
   	
   	/**
   	 * 
   	 * @param {String} value
   	 */
   	setProjectWholeWorld: function (value) {
   		this.data.projectWholeWorld = value;
   	},
   	
   	/**
   	 * 
   	 * @param {Number} value
   	 */
   	setTileSizeInPixel: function (value) {
   		this.data.tileSizeInPixel = parseInt(value);
   	}, 
   	
   	/**
   	 * It is retrieved from <b>host</b> property.
   	 * @return {String} Url string for <b>login</b> post.
   	 */
	getLoginUrl: function (){
		if (!Ext.isEmpty(this.getHost())) {
			this.data.host = this.getHost().trim();  
		} else {
			return "";
		}
		
		var result = this.getHost() + (this.getHost().endsWith("/") ? "": "/") + _LOGIN_EXT;
		return result;
	},
	
	/**
	 * It is retrieved from <b>host</b> property.
	 * @return {String} Url string for <b>runXml</b> post.
	 */
	getRunXmlUrl: function (){
		if (!Ext.isEmpty(this.getHost())) {
			this.data.host = this.getHost().trim();  
		} else {
			return "";
		}
		return this.getHost() + (this.getHost().endsWith("/") ? "": "/") + _RUNXML_EXT;
	},
	
	/**
	 * It is retrieved from <b>host</b> property.
	 * @return {String} Url string for <b>isValid</b> post.
	 */
	getIsValidUrl: function (){
		if (!Ext.isEmpty(this.getHost())) {
			this.data.host = this.getHost().trim();  
		} else {
			return "";
		}
		return this.getHost() + (this.getHost().endsWith("/") ? "": "/") + _ISVALID_EXT;
	},
	
	/**
	 * 
	 * @return {String}
	 */
	getGetMapProxyUrl: function (){
		if (Ext.isEmpty(this.data.getMapProxyUrl)) {
			this.data.getMapProxyUrl = NG.util.parseURL(this.getHost()).getBaseUrl() + "webgissdk_web/NetcadUI.axd";
		}
		return this.data.getMapProxyUrl;
	},
	
	/**
	 * Icon url is appended to host.
	 * 
	 * @param iconType
	 * @return {String} Icon url
	 */
	getIconUrl: function(iconType) {
		if (Ext.isEmpty(iconType)) iconType = "";
		return this.getHost() + (this.getHost().endsWith("/") ? "": "/") + _ICONTYPE_EXT + iconType;
	},
	
	/**
	 * It is calculated as (right - left) / tileSize[256].
	 * Tilesize is been gotten from application settings.
	 * 
	 * @returns {Number}
	 */
	getMaxResolution: function() {
		return (this.getBoundsRight() - this.getBoundsLeft()) / app.getSettings().getTileSize();
	},
	
	/**
	 * Meters or geographic.<br>
	 * For now, <b>m</b> is returned. It must be changed according to project.
	 * If projection of project is geographic, it must be converted to meters.
	 * //TODO
	 * 
	 * @return {String}
	 */
	getUnits: function () {
		return "m";
	},
	
	/**
	 * For now, 4326 is returned. It must be changed according to project.
	 * //TODO 
	 * @return {String}
	 */
	getSRID: function () {
		//TODO projeye göre değişecek
		return "4326";
	}
   	
});

/**
 * @singleton NG.stores.workspace
 */
NG.stores.workspace = new Ext.data.Store({
    model: "NG.models.Workspace",
    sorters: 'name',
    getGroupString : function(record) {
        return record.get('name')[0];
    },
    
    /**
     * If id is empty no action performed
     * 
     * @param {String} id
     */
    removeWorkspaceById: function (id) {
    	if (!Ext.isEmpty(id))
    		this.remove(this.getById(id));

    }
});